package pt.inescid.components.policyengine.heimdall.pap.base;

import java.util.Vector;

import pt.inescid.components.policyengine.heimdall.base.EventObj;
import pt.inescid.components.policyengine.heimdall.base.User;
import pt.inescid.components.policyengine.heimdall.commons.Utilities;

public class SimpleDefinition extends RuleDefinition {

	
//  ----------------------------------------------------------
//  ------------------------   variables   ------------------------
//  ----------------------------------------------------------
	protected String value;

	//  ----------------------------------------------------------
//  ------------------------   constructors   ------------------------
//  ----------------------------------------------------------
	public SimpleDefinition(int type, String valueType, 
			RuleDefinition previousLevel, 
			String value) {
		super(type, previousLevel);
		
		if (valueType != null) {
			for (int i = 0; i < EXPRESSION_VALUE_CODES.length; i++) {
				if (valueType.equals(EXPRESSION_VALUE_CODES[i])) {
					this.valueType = i;
				}
			}
		}
		
		this.value = value;
	}

	public String getValue() {
		return (Utilities.substKeywords(value));
	}

//  ----------------------------------------------------------
//  ------------------------   getFunction   ------------------------
//  ----------------------------------------------------------
	public String getFunction() {
		String result = "";
		
		switch (valueType) {
		case STRING_VALUE:
			result += 
"		String " + getTemporaryVar() + " = \"" + value + "\";\n";
			break;
		case INT_VALUE:
			result += 
"		int " + getTemporaryVar() + " = " + value + ";\n";
			break;
		case BOOLEAN_VALUE:
			result += 
"		boolean " + getTemporaryVar() + " = " + value + ";\n";
			break;

		default:
			break;
		}

		return (result);
	}

//  ----------------------------------------------------------
//  ------------------------   toString   ------------------------
//  ----------------------------------------------------------
	public String toString() {
		String result = "";
		
		result = value;
		
		return (result);
	}

//  ----------------------------------------------------------
//  ------------------------   toString   ------------------------
//  ----------------------------------------------------------
	public String toString(int tabAdvance) {
		String result = "";
		
		for (int i = 0; i < tabAdvance; i++) {
			result += "\t";
		}
		
		result += value;
		
		return (result);
	}

//  ----------------------------------------------------------
//  ------------------------   getParameters   ------------------------
//  ----------------------------------------------------------
	public Vector getParameters() {
		return (null);
	} // getParameters

//  ----------------------------------------------------------
//  ------------------------   getTimeIndependentCopy   ------------------------
//  ----------------------------------------------------------
	public RuleDefinition getTimeIndependentCopy() {
		SimpleDefinition result = new SimpleDefinition(
				type, 
				RuleDefinition.EXPRESSION_VALUE_CODES[valueType], 
				previousLevel, value);
		
		return (result);
	} // getTimeIndependentCopy

//  ----------------------------------------------------------
//  ------------------------   getCeIndependentCopy   ------------------------
//  ----------------------------------------------------------
	public RuleDefinition getCeIndependentCopy() {
		SimpleDefinition result = new SimpleDefinition(
				type, 
				RuleDefinition.EXPRESSION_VALUE_CODES[valueType], 
				previousLevel, value);
		
		return (result);
	} // getCeIndependentCopy

	
	
//  ----------------------------------------------------------
//  ------------------------   getObligationIndependentCopy   ------------------------
//  ----------------------------------------------------------
	public RuleDefinition getObligationIndependentCopy() {
		SimpleDefinition result = new SimpleDefinition(
				type, 
				RuleDefinition.EXPRESSION_VALUE_CODES[valueType], 
				previousLevel, value);
		
		return (result);
	} // getObligationIndependentCopy


//  ----------------------------------------------------------
//  ------------------------   getObligation   ------------------------
//  ----------------------------------------------------------
	public RuleDefinition getObligation() {
		SimpleDefinition result = new SimpleDefinition(
				type, 
				RuleDefinition.EXPRESSION_VALUE_CODES[valueType], 
				previousLevel, value);
		
		return (result);
	} // getObligation


	
	
	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 ============= getSqlRestriction =====================
	 =============                    =============================
	 ==============================================================
	 =========================================================== */
	public String getSqlRestriction(
			User currentUser, EventObj ce) {
		
		String restriction = "";
		
		switch (valueType) {
		case RuleDefinition.STRING_VALUE:
			restriction += "'" + value + "'";
			break;

		case RuleDefinition.BOOLEAN_VALUE:
			restriction += "'1" + 
					(value.equals("true") ? "=" : "<>") + "1'";
			break;

		case RuleDefinition.TIMESTAMP_VALUE:
			restriction += "'" + 
					Utilities.getCompleteFormattedDateString(
							Long.parseLong(value)) + "'";
			break;

		default:
			restriction = value;
			break;
		}
			
		return (restriction);
	} // getSqlRestriction

	
//  ----------------------------------------------------------
//  ------------------------   isObligationDependent   ------------------------
//  ----------------------------------------------------------
	public boolean isObligationDependent() {
		return (false);
	} // isObligationDependent


	
	
}; // SimpleExpression
